package com.woniuxy.service.controller;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.woniuxy.commons.entity.Order;
import com.woniuxy.service.configuration.AliPayConfig;
import com.woniuxy.service.service.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;


@RequestMapping("/payorder")
@Controller
public class PayController {
    @Resource
    private OrderService orderService;

    @RequestMapping("/pay")
    public void pay(Integer orderId, HttpServletResponse response) throws Exception {

        Order order=orderService.findbyid(orderId);

        //完成支付
//        boolean pay=orderService.pay(orderId);
        //-----------------支付款支付-------------------
        // 获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AliPayConfig.gatewayUrl, AliPayConfig.app_id,
                AliPayConfig.merchant_private_key, "json", AliPayConfig.charset, AliPayConfig.alipay_public_key,
                AliPayConfig.sign_type);
        // 设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AliPayConfig.return_url);
        alipayRequest.setNotifyUrl(AliPayConfig.notify_url);
        // 商户订单号，商户网站订单系统中唯一订单号，必填
//				String out_trade_no = new String(orderno.getBytes("ISO-8859-1"), "UTF-8");
//        String out_trade_no = order.getNo();
        String out_trade_no = new Date().getTime()+"";

        orderService.setorderno(out_trade_no,orderId);
        // 付款金额，必填
//				String total_amount = new String(money.getBytes("ISO-8859-1"), "UTF-8");
        String total_amount = order.getServiceItem().getPrice().toString();
        // 订单名称，必填
//				String subject = new String(("no"+orderno).getBytes("ISO-8859-1"), "UTF-8");
        String subject = "no"+order.getNo();
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"total_amount\":\"" + total_amount
                + "\"," + "\"subject\":\"" + subject + "\"," + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String result = null;
        result = alipayClient.pageExecute(alipayRequest).getBody();
        response.setContentType("text/html");
        response.getWriter().write(result);
    }
    //异步通知
    public void asynchroPayEnd(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("异步通知");

    }
    //同步通知
    @RequestMapping("/synchroPayEnd")
    public void synchroPayEnd(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String parameter = request.getParameter("out_trade_no");
        orderService.paybyno(parameter);
            response.sendRedirect("http://localhost:8080/disabledterminal/orderterminal");

    }
}